﻿/*********************************************************************************
 * LinqToLfsWorld v1.0
 * Author: Steve Hobbs
 * Email: steve@elkdanger.co.uk
 * Url: http://www.elkdanger.co.uk
 * Project Url: http://www.codeplex.com/linqtolfsworld
 * 
 * Example website demonstrating some aspects of retrieving data from the LfsWorld Pubstat service.
 * 
 * Copyright (C) Steve Hobbs 2008
 * Distributed under the GNU General Public License (GPL) v2 (see http://www.codeplex.com/linqtolfsworld/license)
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinqToLfsWorld;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

		if (!IsPostBack)
		{

			/**
			 * Before you begin, either set your identkey in the constructor of LfsWorldContext below and delete
			 * the 'identKey' attribute from the configuration section in the web.config file, or
			 * update the configuration section in the web.config file with your own identkey setting.
			 * 
			 * Also, you should always use this 'using' construct with LfsWorldContext, or at least make sure
			 * you call Dispose when you are done.
			 */
			using (LfsWorldContext context = new LfsWorldContext())
			{

				// Make sure caching is enabled
				context.CachingEnabled = true;

				// Handle any "request made" events
				context.RequestMade += new LfsRequestHandler(context_RequestMade);

				/* Make an initial query to the hosts action */
				var hostsList = (from host in context.Hosts select host).ToList();

				// Refine further - only show active hosts
				var refinedHosts = from host in hostsList
								   orderby host.HostName
								   select new
								   {
									   host.HostName,
									   host.TrackName,
									   host.RacerCount,
									   host.Country
								   };

				// Bind to the grid view
				LfsHostsGridView.DataSource = refinedHosts;
				LfsHostsGridView.DataBind();
			}	 

		}

    }

	/// <summary>
	/// Fired by the context whenever a request is made.
	/// </summary>
	void context_RequestMade(object sender, LfsWorldResponse response)
	{
		this.Response.Write("Cached response: " + response.FromCache + "<br/>");
		this.Response.Write("Request: " + response.RequestUrl + "<br/>");
	}
}
